﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Office.Interop.Excel;
using System.IO;



namespace project_forgotten_tuba
{
    public class Container
    {
        public bool IsClient { get; set; }
        public bool IsServer { get; set; }



        public GameState ClientGameState { get; set; }
        public GameClient gameClient { get; set; }
        public Player ClientPlayer
        {
            get
            {
                if (ClientGameState == null || ClientPlayerIndex > ClientGameState.AllPlayers.Count)
                    return null;
                else
                    return ClientGameState.AllPlayers[ClientPlayerIndex - 1];

            }
        }
        public int ClientPlayerIndex { get; set; }
        public string HostIP { get; set; }

        public GameServer gameServer { get; set; }


        public Container()
        {
            ClientGameState = new GameState();

            ClientGameState.MainContainer = this;

            Console.WriteLine("Select application mode: \n" +
                              "0. Host a game\n" +
                              "1. Join a game");
            char option = '-';
            while (!(option == '0' || option == '1' || option == '2'))
            {
                option = Console.ReadKey(true).KeyChar;
                if (option == '0')
                {
                    IsClient = true;
                    IsServer = true;
                    Console.WriteLine("Hosting Game");
                }
                if (option == '1')
                {
                    IsClient = true;
                    IsServer = false;
                    Console.WriteLine("Joining game");
                }
            }
            Console.WriteLine("");

            Console.WriteLine("Player index? ");
            option = '-';
            while (!(option == '1' || option == '2'))
            {
                option = Console.ReadKey(true).KeyChar;
                if (option == '1')
                {
                    GameStatic.PlayerIndex = 1;
                    ClientPlayerIndex = 1;
                    Console.WriteLine("Player 1");
                }
                if (option == '2')
                {
                    GameStatic.PlayerIndex = 2;
                    ClientPlayerIndex = 2;
                    Console.WriteLine("Player 2");
                }
            }

            if (IsServer)
            {
                HostIP = "127.0.0.1";
            }
            if (IsClient && !IsServer)
            {
                Console.WriteLine("Enter host IP: ");
                HostIP = Console.ReadLine();
            }




            if (IsServer)
            {
                //ServerGameState = new GameState() { MainContainer = this };

                gameServer = new GameServer() { MainContainer = this };
                gameServer.Init();
            }

            if (IsClient)
            {
                ClientGameState = new GameState() { MainContainer = this };

                gameClient = new GameClient() { MainContainer = this };
                gameClient.Init();
            }
        }
        public List<string> ReadLocalDeck()
        {
            List<string> lines;
            if (File.Exists("deck\\deck.txt"))
            {
                lines = File.ReadAllLines("deck\\deck.txt").ToList();
            }
            else
            {
                lines = new List<string>();
            }

            return lines;
        }

        public void Update(float elapsed)
        {
            if (IsServer)
            {
                gameServer.HandleAllMessages();
            }


            if (IsClient)
            {
                if (gameClient.IsConnected == false)
                {
                    gameClient.Connect(HostIP, 14242);
                }

                gameClient.HandleAllMessages();

                if (ClientPlayer != null)
                {
                    HandleInput();
                }

                if (ClientGameState != null)
                    ClientGameState.Update(elapsed);
            }
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            ClientGameState.Draw(spriteBatch);
        }

        public void HandleInput()
        {
            HandleMouseInput();
            HandleKeyboardInput();
        }

        public enum InputType { LeftClick, LeftRelease, LeftDrag, Flip, Tap, Shuffle } 

        private MouseState PreviousMouseState { get; set; }
        private int leftmousedownx { get; set; }
        private int leftmousedowny { get; set; }
        private int rightmousedownx { get; set; }
        private int rightmousedowny { get; set; }
        public void HandleMouseInput()
        {
            if (PreviousMouseState == null)
            {
                PreviousMouseState = Mouse.GetState();
                return;
            }
            MouseState CurrentMouseState = Mouse.GetState();

            int mousex = CurrentMouseState.X;
            int mousey = CurrentMouseState.Y;
            

            if (CurrentMouseState.LeftButton == ButtonState.Pressed &&
             !(PreviousMouseState.LeftButton == ButtonState.Pressed))
            {
                leftmousedownx = mousex;
                leftmousedowny = mousey;

                //HERE: code for pressing left button
                //ClientGameState.AllPlayers[GameStatic.PlayerIndex-1].HandleLeftPress(mousex, mousey);
                if (gameClient.IsConnected)
                    gameClient.SendInputMessage(ClientPlayerIndex, InputType.LeftClick, mousex, mousey);

                //select card
            }
            else if (PreviousMouseState.LeftButton == ButtonState.Pressed &&
                    !(CurrentMouseState.LeftButton == ButtonState.Pressed))
            {
                int leftdragx = mousex - leftmousedownx;
                int leftdragy = mousey - leftmousedowny;

                //HERE: code for releasing left button
                //ClientGameState.AllPlayers[GameStatic.PlayerIndex - 1].HandleLeftRelease(mousex, mousey);
                if (gameClient.IsConnected)
                    gameClient.SendInputMessage(ClientPlayerIndex, InputType.LeftDrag, mousex, mousey);
                if (gameClient.IsConnected)
                    gameClient.SendInputMessage(ClientPlayerIndex, InputType.LeftRelease, mousex, mousey);

                //deselect card
                //send updated position to players
            }
            else if (PreviousMouseState.LeftButton == ButtonState.Pressed &&
                      CurrentMouseState.LeftButton == ButtonState.Pressed)
            {
                //int leftdragx = mousex - leftmousedownx;
                //int leftdragy = mousey - leftmousedowny;

                //HERE code for dragging left button
                ClientGameState.AllPlayers[ClientPlayerIndex - 1].HandleLeftDrag(mousex, mousey);
                //move card locally
            }



            if (CurrentMouseState.RightButton == ButtonState.Pressed &&
             !(PreviousMouseState.RightButton == ButtonState.Pressed))
            {
                rightmousedownx = mousex;
                rightmousedowny = mousey;

                //HERE: code for pressing right button
            }
            else if (PreviousMouseState.RightButton == ButtonState.Pressed &&
                    !(CurrentMouseState.RightButton == ButtonState.Pressed))
            {
                //HERE: code for releasing right button
            }
            else if (PreviousMouseState.RightButton == ButtonState.Pressed &&
                      CurrentMouseState.RightButton == ButtonState.Pressed)
            {
                int rightdragx = mousex - rightmousedownx;
                int rightdragy = mousey - rightmousedowny;

                //HERE code for dragging right button
            }

            PreviousMouseState = CurrentMouseState;
        }


  

        private KeyboardState PreviousKeyboardState { get; set; }
        public void HandleKeyboardInput()
        {
            if (PreviousKeyboardState == null)
            {
                PreviousKeyboardState = Keyboard.GetState();
                return;
            }
            KeyboardState CurrentKeyboardState = Keyboard.GetState();


            foreach (Keys k in Enum.GetValues(typeof(Keys)))
            {
                if (CurrentKeyboardState.IsKeyDown(k) && PreviousKeyboardState.IsKeyUp(k))
                {
                    //code for pressing keys

                    if (k == Keys.T)
                    {
                        //code for pressing T
                        //ClientGameState.AllPlayers[GameStatic.PlayerIndex - 1].HandleTapCard();
                        if (gameClient.IsConnected)
                            gameClient.SendInputMessage(ClientPlayerIndex, InputType.Tap, 0, 0);

                        //if a card is selected, tap it and send updated information to players
                    }

                    if (k == Keys.F)
                    {
                        //code for pressing T
                        //ClientGameState.AllPlayers[GameStatic.PlayerIndex - 1].HandleFlipCard();
                        if (gameClient.IsConnected)
                            gameClient.SendInputMessage(ClientPlayerIndex, InputType.Flip, 0, 0);

                        //if a card is selected, tap it and send updated information to players
                    }

                    if (k == Keys.E)
                    {
                        //code for pressing s
                        //ClientGameState.Shuffle();

                        if (gameClient.IsConnected)
                            gameClient.SendInputMessage(ClientPlayerIndex, InputType.Shuffle, 0, 0);

                        //if a card is selected, tap it and send updated information to players
                    }

                    /*
                    if (k == Keys.C)
                    {
      

                            Dictionary<string, string> dict = new Dictionary<string, string>();

                            GameStatic.excel_init("excel_tutorial.xlsx");

                            for (int i = 1; i < 5; i++)
                                {
                        
                                    string CurrentTag = "A"+i.ToString();
                                    string CurrentDef = "B" + i.ToString();

                                    string Current2Tag = GameStatic.excel_getValue(CurrentTag);
                                    string Current2Def = GameStatic.excel_getValue(CurrentDef);

                                    dict.Add(Current2Tag, Current2Def);

                        
                                }
                            foreach (var pair in dict)
                                {
                                    Console.WriteLine("{0}, {1}", pair.Key,
                                    pair.Value);
                                
                                }
                 
                    }
                */
               }


            }

            PreviousKeyboardState = CurrentKeyboardState;
        }


    }
}
    

